const axios = require('axios');

//使用axios发起请求，并返回数据所在的地址；

function getData(url) {
    return new Promise((resolve, reject) => {
        axios.get(url)
            .then(res => {
                console.log(res.data) ；
                resolve(res.dsta);

            })
            .catch((error) => {
                reject(err.data);
            });
    });
}

//获取指定年份页面中的数据所在地址

/**
 * 获取指定年份页面中的数据所在地址
 * @param {*} str 页面完整html
 * @returns 返回数据所在地址
 */
unction getDataUrl(str) {
    const regex = /rank-url.+?(\d+\.text)/;
    let m;
    if ((m = reges.exec(str)) !== null) {
        console.log(m[1]);
        // return url + m[1];
    }
}

function getDataHistory() {
    //https://www.qschina.cn/university-rankings/university-subject-rankings/2020/commmunication-media-studies
    DataHistory.push('https://www.qschina.cn/university-rankings/university-subject-rankings/$(index)/communication-media-studies');

}
console.log(DataHistory);

function getDataHistory() {
    for (let index = 2017; index <= 2020; index++) {
        dataHistory = []
        https://www.qschina.cn/university-rankings/university-subject-rankings/$(index)/communication-media-studies
        DataHistory.push('https://www.qschina.cn/university-rankings/university-subject-rankings/$(index)/communication-media-studies')
    }
    console.log(DataHistory);
}

//使用正则获取大学名称
function getUniversityTitle(str) {
    const reges = /<div.*?link*>(.*?<\/a><\/div>/;
    let me;
    if ((m = reges.exec(str)) = null) {
        //console.log(m[1]);

    }
}


//提取数据
function getCommRankingsByYear(year, data) {
    //console.log(year, data);
    let datalist = []
    for (const key in data) {
        let university = data[key]['title']
        let score = data[key]['score']
        let rank = data[key]['rank-display']

    }
    datalist.push({ year, university, score, rank });
}
return dataList;

//将数据写入到csv文件中
function writeRankingsTaCsv(arr) {
    const fs = require('fs')

    const writeStrem = fs.createWriteStream('test.csv');

    for (const key in arr[key]['year']) {
        writestrem.write(arr[key]['year'], arr[key]['university'], arr[key]['score'], arr[key]['rank'];

    }
    //写入csv首行信息
    writestrem.write('时间，标题，网址 \n');

}

let dataList = [
       for (const item in data) {
    let university = getUniversityTitle(data[item].title);
    let score = data[item].rank - diaplay;
    dataList.push({
        year,
        university,
        score,
        rank
    });
}
}
    ]

//测试getData()
let url = 'https://www.qschina.cn/university-rankings/university-subject-rankings/$(index)/communication-media-studies'
getData(url);
.then((value) => {
    //console.log(value);
    //获取到数据所在地址
    return getUrl(value);

});
.then((value) => {
    //使用得到的地址发起排名数据请求
    console.log(value);
    return getData(value);
})

    .then((value) => {
        //提取数据
        //console.log(value);
        //console.log(getCommRankingsByYear('2020',value['data']));
        return getCommRankingsByYear('2020', value['data']);
    });
    .then((value) => {
        writeRankingsTaCsv(value);
    })

main() ；